﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oracle.ManagedDataAccess.Client;
using System.Configuration;
using System.Data;

namespace ThanhTu.GNC.Media.KQXS.DataAccess.DAL
{
    public class BaseDAL : IDisposable
    {
        private bool disposed = false;
        private OracleConnection oraConn = null;
        private string connStr = string.Empty;

        public BaseDAL() { connStr = ConfigurationManager.ConnectionStrings["GNCMedia.KQXS"].ConnectionString; }
        ~BaseDAL() { Dispose(false); }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    if (oraConn != null)
                    {
                        oraConn.Close();
                        oraConn.Dispose();
                        oraConn = null;
                    }
                }

                disposed = true;
            }
        }

        protected DataSet GetDataSet(OracleCommand oraComm)
        {
            using (oraConn = new OracleConnection(connStr))
            {
                oraComm.Connection = oraConn;
                DataSet ds = null;

                try
                {
                    using (OracleDataAdapter oraDataAdp = new OracleDataAdapter(oraComm))
                    {
                        ds = new DataSet();
                        oraDataAdp.Fill(ds);
                    }
                }
                catch (Exception ex)
                {
                    if (!ex.Message.Equals("Invalid operation on a closed object"))
                    { throw ex; }
                }

                return ds;
            }
        }

        protected int ExecuteNoneQuery(OracleCommand oraComm)
        {
            using (oraConn = new OracleConnection(connStr))
            {
                int result = 0;
                oraComm.Connection = oraConn;

                try
                {
                    oraConn.Open();
                    result = oraComm.ExecuteNonQuery();
                    oraConn.Close();
                }
                catch (OracleException ex) { throw ex; }

                return result;
            }
        }

        protected object ExecuteScalar(OracleCommand oraComm)
        {
            using (oraConn = new OracleConnection(connStr))
            {
                object result = null;
                oraComm.Connection = oraConn;

                try
                {
                    oraConn.Open();
                    result = oraComm.ExecuteScalar();
                    oraConn.Close();
                }
                catch (OracleException ex) { throw ex; }

                return result;
            }
        }
    }
}
